﻿using Activity.Data;
using System;
using System.Linq;
using static Activity.Comm.CheckUserData;

namespace Activity.API
{
    public class UserPlayHandler
    {
        /// <summary>
        /// 检查用户是否注册并返回抽奖次数
        /// </summary>
        /// <param name="openID"></param>
        /// <returns></returns>
        public static ResultData CheckUser(string openID)
        {
            var dc = new HKEntities();
            var qt = dc.KM_V_CardBinding.Where(t => t.OpenID == openID);
            int count = 0;
            var result = new ResultData();
            if (qt.Any())
            {
                //已注册，获取抽奖次数。
                count = UserPlayCount(openID);
                result.isRegister = true;
                result.Count = count;
            }
            else
            {
                //直接返回
                result.isRegister = false;
                result.Count = count;
            }
            Log.WriteLog($"检查用户是否注册并返回抽奖{result.Count}次数");
            return result;
        }
        /// <summary>
        /// 验证小票
        /// </summary>
        public static int CheckNote(string openID,string posCode)
        {
            var dc = new ActivityEntities();
            var query = dc.HD_V_PosList.Where(t => t.openid == openID && t.billno == posCode);
            if (!query.Any()) return -1;
            var count = 0;
            var recInfo = new HD_Receipt
            {
                PosCode = posCode,
                OpenID = openID,
                CreateTime = DateTime.Now
            };
            dc.HD_Receipt.Add(recInfo);
            try
            {
                dc.SaveChanges();
                count = UserPlayCount(openID);
                return count;
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                return -1;
            }
        }
    }
}
